Repository navigation
pnbd dyncov LL: fix jumps of the likelihood close to z = 1 (GSL 2F1 returns GSL_EUNIMPL) - #302
Open
bachmannpatrick wants to merge 1 commit into
Open
bachmannpatrick wants to merge 1 commit into
bachmannpatrick wants to merge 1 commit into
Conversation
…he transformation z -> 1-z gsl_sf_hyperg_2F1_e() returns GSL_EUNIMPL with value 0 for z >= 0.995 if r+s+x >= 10. Only GSL_EMAXITER and GSL_EDOM were handled, so the 0 entered the likelihood and individual log-likelihoods jumped. For z >= 0.995, or if GSL does not report success, the terms are now computed from the linear transformation z -> 1-z (A&S 15.3.6), which is elementary plus a short series because c = a+1. Also divide on the log scale if beta^(r+s+x) overflows. Results for z < 0.995 are unchanged. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UTYLyr3kUoxePkWEp1bmB4
Owner
Author
|
Check in the built package The branch was installed on a Linux server (
No value is infinite or missing in either run, and the run time of the scan is unchanged (15 and 29 minutes). Checks of this pull request
Generated by Claude Code |
bachmannpatrick
marked this pull request as ready for review
November 17, 2026 14:49
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The issue
The log-likelihood of the Pareto/NBD with dynamic covariates is not continuous. For single customers it jumps by 1 to 13 units, and optimizers with numerical gradients stop at a jump with "false convergence".
pnbd_dyncov_LL_i_hyp_beta_g_alpha()andpnbd_dyncov_LL_i_hyp_alpha_ge_beta()compute terms of the form2F1(r+s+x, b; r+s+x+1; z) / beta^(r+s+x)withgsl_sf_hyperg_2F1_e().z >= 0.995andr+s+x >= 10, GSL returns status 24 (GSL_EUNIMPL) and the value 0. Just below either limit it is accurate to 13 digits.GSL_EMAXITERandGSL_EDOM. For status 24 it takes the 0, so one term of the customer's likelihood disappears.To reproduce:
gsl_sf_hyperg_2F1_e(17.61, 9.62, 18.61, 0.995, &res)returns status 24 andres.val = 0(GSL 2.7.1). The true value is 1.3886e+20.The region is reached when beta is large relative to alpha (z = 0.995 means beta is about 200 times alpha) and the customer has many transactions or s is large. In simulated data (20,000 customers, r 0.62, alpha 5, s 2.7, beta 818), 133 customers have a jump when s is varied from 1.5 to 8 at a fixed mean attrition rate, exactly where
(beta - alpha) / (beta + t_x) = 0.995or wherer+s+x = 10. In an application with real data about 0.9 % of the customers were affected and no fit converged.Two related defects:
pow(beta, r+s+x)overflows and the quotient becomes 0 although the true value is representable (for example x = 150 at beta = 3,000).r+x(or integersin the other case), GSL reports success from z = 0.995 on but is inaccurate, in some cases with the wrong sign.The change
For
z >= 0.995, or whenever GSL does not report success, the terms are computed from the linear transformation z -> 1-z (Abramowitz and Stegun 15.3.6). Because c = a+1, one of the two hypergeometric functions on the right-hand side is elementary. With w = 1-z:The series converges like w^n. Both parts are evaluated on the log scale after division by the power, so nothing overflows. Both have a pole at integer b that cancels in the sum; within 1e-4 of an integer the value is interpolated from four neighbouring values of b.
Order of evaluation in the new helper
pnbd_dyncov_LL_i_hyp_term():z < 0.995: GSL as before. Results are bit-identical wherever GSL reports success. If the power overflows, the division is done on the log scale.Validation
The two functions were compiled on their own next to the original code (g++, C++11 and C++17, GSL 2.7.1) and compared with a reference computed with 80 digits (mpmath) on about 4,400 random parameter sets (r from 0.2 to 3, s from 0.2 to 12, x from 0 to 150, including integer r and s).
src/test-cpp.cppwith eight reference values. The original code fails six of them.Status
Draft. The package has not been built with this change and its test suite has not been run locally; the checks of this pull request are the first build. Only GSL 2.7.1 was tested.
NEWS.mdis not updated.Open questions
pnbd.cppcalls the same GSL function throughclv::vec_hyp2F1()without looking at the status; the affected customers come out as NaN. The helper could be reused there. Not part of this pull request.Generated by Claude Code